<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>放大镜</title>
    <style type="text/css">
        * {
            margin: 0;
            padding: 0;
        }

        .fdj {
            width: 960px;
            height: 540px;
            margin: 0 auto;
            display: flex;
        }

        .left {
            width: 480px;
            height: 270px;
            background-image: url("../img/ysx.jpg");
            background-size: 480px;
            position: relative;
        }

        .zhezhaoceng {
            width: 240px;
            height: 135px;
            background-color: rgba(255, 255, 0, 0.5);
            display: none;
            position: absolute;
        }

        .right {
            width: 480px;
            height: 270px;
            background-image: url("../img/ysx.jpg");
            background-size: 960px;
            display: none;
        }
    </style>
</head>
<body>
<div class="fdj">
    <div class="left">
        <div class="zhezhaoceng"></div>
    </div>
    <div class="right"></div>
</div>
<script>
    var fdj = document.querySelector(".fdj");
    var left = document.querySelector(".left");
    var right = document.querySelector(".right");
    var zhezhaoceng = document.querySelector(".zhezhaoceng");
    //鼠标移入事件
    left.onmouseenter = function () {
        zhezhaoceng.style.display = "block";
        right.style.display = "block";
    }
    //鼠标在左边盒子移动事件
    left.onmousemove = function (e) {
        /*
        * 重难点
        * */
        var x = e.pageX - 120 - fdj.offsetLeft;
        var y = e.pageY - 67.5 - fdj.offsetTop;
        //判断限定出界
        if (x < 0) {
            x = 0;
        }
        if (x > 240) {
            x = 240;
        }
        if (y < 0) {
            y = 0;
        }
        if (y > 135) {
            y = 135;
        }
        zhezhaoceng.style.left = x + "px";
        zhezhaoceng.style.top = y + "px";

        right.style.backgroundPositionX = -2 * x + "px";
        right.style.backgroundPositionY = -2 * y + "px";
    }
    //鼠标移出事件
    left.onmouseleave = function () {
        zhezhaoceng.style.display = "none";
        right.style.display = "none";
    }
</script>
</body>
</html>